import OperatorCode from './operatorCode.js'
import opConfig from './operatorConfig.js'
import Log from '../util/log.js'

const TAG = 'JS/Operator/BLE: '

var getRequestAction = function (requestCode) {
    return {
        bundleName: opConfig.OP_BUNDLE_NAME,
        abilityName: opConfig.BLE_ABILITY_NAME,
        abilityType: OperatorCode.ABILITY_TYPE_INTERNAL,
        syncOption: OperatorCode.ACTION_SYNC,
        messageCode: requestCode,
    };
};

export default {
    data: {},
    openBluetoothAdapter: async function () {
        let action = getRequestAction(OperatorCode.OPEN_BLUETOOTH_ADAPTER);
        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'openBluetoothAdapter result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    closeBluetoothAdapter: async function () {
        let action = getRequestAction(OperatorCode.CLOSE_BLUETOOTH_ADAPTER);
        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'closeBluetoothAdapter result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    getBluetoothAdapterState: async function () {
        let action = getRequestAction(OperatorCode.GET_BLUETOOTH_ADAPTER_STATE);
        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'getBluetoothAdapterState result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    onBluetoothAdapterStateChange: async function (callback) {
        let action = getRequestAction(OperatorCode.ON_BLUETOOTH_ADAPTER_STATE_CHANGE);
        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        Log.debug(TAG + 'onBluetoothAdapterStateChange result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    startBluetoothDevicesDiscovery: async function (services, mac, allowDuplicatesKey, interval) {
        let action = getRequestAction(OperatorCode.START_BLUETOOTH_DEVICES_DISCOVERY);
        let actionData = {};
        actionData.services = services;
        actionData.macAdress = mac;
        actionData.allowDuplicatesKey = allowDuplicatesKey;
        actionData.interval = interval;
        action.data = actionData;

        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'startBluetoothDevicesDiscovery result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    stopBluetoothDevicesDiscovery: async function () {
        let action = getRequestAction(OperatorCode.STOP_BLUETOOTH_DEVICES_DISCOVERY);
        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'stopBluetoothDevicesDiscovery result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    onBluetoothDeviceFound: async function (callback) {
        let action = getRequestAction(OperatorCode.ON_BLUETOOTH_DEVICE_FOUND);
        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        Log.debug(TAG + 'onBluetoothDeviceFound result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    getDeviceId: async function () {
        let action = getRequestAction(OperatorCode.GET_DEVICE_ID);
        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'getDeviceId result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    createBleConnection: async function (deviceId) {
        let action = getRequestAction(OperatorCode.CREATE_BLE_CONNECTION);
        let actionData = {};
        Log.debug(TAG + "bleOperator createBleConnection deviceId = " + deviceId);
        actionData.deviceId = deviceId.toUpperCase();
        action.data = actionData;

        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'createBleConnection result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    closeBleConnection: async function (deviceId) {
        let action = getRequestAction(OperatorCode.CLOSE_BLE_CONNECTION);
        let actionData = {};
        Log.debug(TAG + "bleOperator closeBleConnection deviceId = " + deviceId);
        actionData.deviceId = deviceId.toUpperCase();
        action.data = actionData;

        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'closeBleConnection result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    onBleConnectionStateChange: async function (callback) {
        let action = getRequestAction(OperatorCode.ON_BLE_CONNECTION_STATE_CHANGE);
        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        Log.debug(TAG + 'onBleConnectionStateChange result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    onBleServicesDiscovered: async function (callback) {
        let action = getRequestAction(OperatorCode.ON_BLE_SERVICES_DISCOVERED);
        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        Log.debug(TAG + 'onBleServicesDiscovered result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    readBleCharacteristicValue: async function (deviceId, serviceId, characteristicId, callback) {
        let action = getRequestAction(OperatorCode.READ_BLE_CHARACTERISTIC_VALUE);
        let actionData = {};
        Log.debug(TAG + "bleOperator readBleCharacteristicValue deviceId = " + deviceId);
        actionData.deviceId = deviceId.toUpperCase();
        actionData.serviceId = serviceId;
        actionData.characteristicId = characteristicId;
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        Log.debug(TAG + 'readBleCharacteristicValue result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    writeBleCharacteristicValue: async function (deviceId, serviceId, characteristicId, data, callback) {
        Log.debug(TAG + deviceId + serviceId + characteristicId + data);
        let action = getRequestAction(OperatorCode.WRITE_BLE_CHARACTERISTIC_VALUE);
        let actionData = {};
        Log.debug(TAG + "bleOperator writeBleCharacteristicValue deviceId = " + deviceId);
        actionData.deviceId = deviceId.toUpperCase();
        actionData.serviceId = serviceId;
        actionData.characteristicId = characteristicId;
        actionData.writeData = data;
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    onBleCharacteristicValueChange: async function (callback) {
        let action = getRequestAction(OperatorCode.ON_BLE_CHARACTERISTIC_VALUE_CHANGE);
        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        Log.debug(TAG + 'onBleCharacteristicValueChange result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    notifyBleCharacteristicValueChange: async function (deviceId, serviceId, characteristicId, state) {
        let action = getRequestAction(OperatorCode.NOTIFY_BLE_CHARACTERISTIC_VALUE_CHANGE);
        let actionData = {};
        //        Log.debug(TAG + "bleOperator notifyBleCharacteristicValueChange deviceId = " + deviceId);
        //        Log.debug(TAG + "bleOperator notifyBleCharacteristicValueChange serviceId = " + serviceId);
        //        Log.debug(TAG + "bleOperator notifyBleCharacteristicValueChange characteristicId = " + characteristicId);
        actionData.deviceId = deviceId.toUpperCase();
        actionData.serviceId = serviceId;
        actionData.characteristicId = characteristicId;
        actionData.state = state;
        action.data = actionData;

        let resultStr = await FeatureAbility.callAbility(action);

        Log.debug(TAG + 'notifyBleCharacteristicValueChange result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    onEnableNotifyIndicate: async function (callback) {
        let action = getRequestAction(OperatorCode.ON_ENABLE_NOTIFY_INDICATE);
        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        Log.debug(TAG + 'onEnableNotifyIndicate result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    setEnableIndication: async function (isEnableIndication) {
        let action = getRequestAction(OperatorCode.SET_ENABLE_INDICATION);
        let actionData = {};
        actionData.isEnableIndication = isEnableIndication;
        action.data = actionData;

        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'setEnableIndication result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    readBleDescriptorValue: async function (deviceId, serviceId, characteristicId, descriptorId,
                                            callback) {
        let action = getRequestAction(OperatorCode.READ_BLE_DESCRIPTOR_VALUE);
        let actionData = {};
        Log.debug(TAG + "bleOperator readBleDescriptorValue deviceId = " + deviceId);
        actionData.deviceId = deviceId.toUpperCase();
        actionData.serviceId = serviceId;
        actionData.characteristicId = characteristicId;
        actionData.descriptorId = descriptorId;
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        Log.debug(TAG + 'readBleDescriptorValue result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    writeBleDescriptorValue: async function (deviceId, serviceId, characteristicId, descriptorId, data,
                                             callback) {
        let action = getRequestAction(OperatorCode.WRITE_BLE_DESCRIPTOR_VALUE);
        let actionData = {};
        Log.debug(TAG + "bleOperator writeBleDescriptorValue deviceId = " + deviceId);
        actionData.deviceId = deviceId.toUpperCase();
        actionData.serviceId = serviceId;
        actionData.characteristicId = characteristicId;
        actionData.descriptorId = descriptorId;
        actionData.writeData = data;
        action.data = actionData;

        let resultStr = await FeatureAbility.subscribeAbilityEvent(action, callbackData => {
            let callbackJson = JSON.parse(callbackData);
            callback(callbackJson);
        });
        Log.debug(TAG + 'writeBleDescriptorValue result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
    getBleConnectionState: async function (deviceId) {
        let action = getRequestAction(OperatorCode.GET_BLE_CONNECTION_STATE);
        let actionData = {};
        Log.debug(TAG + "bleOperator getBleConnectionState deviceId = " + deviceId);
        actionData.deviceId = deviceId.toUpperCase();
        action.data = actionData;

        let resultStr = await FeatureAbility.callAbility(action);
        Log.debug(TAG + 'getBleConnectionState result is:' + resultStr);
        let resultObj = JSON.parse(resultStr);
        return resultObj;
    },
}